package com.www.project.meetschool.service;


import com.www.common.utils.StringUtils;
import com.www.project.meetschool.domain.MeetEvent;
import com.www.project.meetschool.domain.Message;
import com.www.project.meetschool.mapper.MeetEventMapper;
import com.www.project.meetschool.mapper.MeetEventUserMapper;
import com.www.project.meetschool.mapper.MessageMapper;
import com.www.project.monitor.domain.MeetEventUser;
import com.www.project.system.domain.SysUser;
import com.www.project.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import java.util.List;

@Service
public class MeetEventServiceImpl implements MeetEventService
{

    @Autowired
    private MeetEventMapper meetEventMapper;

    @Autowired
    private MeetEventUserMapper meetEventUserMapper;

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public int add(MeetEvent event)
    {
        int row = meetEventMapper.insertEvent(event);

        MeetEventUser eventUser = new MeetEventUser();
        eventUser.setEventId(event.getEventId());
        eventUser.setUserId(event.getCreateId());
        joinEvent(eventUser);
        return row;
    }

    @Override
    public List<MeetEvent> selectEventList(MeetEvent event)
    {
/*        if(StringUtils.isEmpty(event.getSearchValue())){
            event.setSearchValue(null);
        }
        if(StringUtils.isEmpty(event.getBeginTime())){
            event.setBeginTime(null);
        }
        if(StringUtils.isEmpty(event.getEndTime())){
            event.setEndTime(null);
        }*/
        return meetEventMapper.selectEventList(event);
    }



    @Override
    public int updateEvent(MeetEvent event)
    {
        return meetEventMapper.updateEvent(event);
    }

    @Override
    public int deleteEventByIds(Long[] eventIds)
    {
        return meetEventMapper.deleteEventByIds(eventIds);
    }

    @Override
    public int joinEvent(MeetEventUser eventUser)
    {

        int row =  meetEventUserMapper.insertEventUser(eventUser);
        if(row==1)
        {
            MeetEvent event = meetEventMapper.selectEventById(eventUser.getEventId());
            event.setJoined(event.getJoined()+1);
            produce("加入",eventUser,event);
            meetEventMapper.updateEvent(event);
        }
        return row;
    }

    @Override
    public MeetEvent selectEventById(Long eventId)
    {
        return meetEventMapper.selectEventById(eventId);
    }




    @Override
    public int exitEvent(MeetEventUser eventUser)
    {
        int row =  meetEventUserMapper.deleteEventUserInfo(eventUser);
        if(row==1)
        {
            MeetEvent event = meetEventMapper.selectEventById(eventUser.getEventId());
            event.setJoined(event.getJoined()-1);
            produce("退出",eventUser,event);
            meetEventMapper.updateEvent(event);
        }
        return row;
    }

    @Override
    public boolean checkPersonNumber(MeetEventUser eventUser)
    {
        MeetEvent  event = meetEventMapper.selectEventById(eventUser.getEventId());
        if(event.getPersonNumber()!=event.getJoined())
        {
            return true;
        }
        return false;
    }



    @Override
    public List<MeetEvent> selectJoinedEventList(MeetEvent event)
    {
        return meetEventMapper.selectJoinedEventList(event);
    }

    @Override
    public boolean checkStatus(MeetEventUser eventUser)
    {
        MeetEvent  event = meetEventMapper.selectEventById(eventUser.getEventId());
        if("未开始".equals(event.getStatus()))
        {
            return true;
        }
        return false;
    }

    @Override
    public List<Message> selectMessageList(Message message)
    {
        return messageMapper.selectMessageList(message);
    }

    private void produce(String operation ,MeetEventUser eventUser ,MeetEvent event)
    {
        Message message = new Message();
        message.setConsumerId(eventUser.getUserId());
        SysUser sysUser = sysUserMapper.selectUserById(eventUser.getUserId());
        if("加入".equals(operation)){
            message.setContent("很高兴，您能加入"+event.getTheme()+"活动");
        }else{
            message.setContent("很遗憾，您退出"+event.getTheme()+"活动");
        }
        message.setEventId(event.getEventId());
        message.setType("1");
        messageMapper.insertMessage(message);
        Message message1 = new Message();
        message1.setConsumerId(event.getCreateId());
        message1.setContent(sysUser.getUserName()+operation+event.getTheme()+"活动,目前人数为:"+event.getJoined()+"人");
        message1.setEventId(event.getEventId());
        message1.setType("1");
        messageMapper.insertMessage(message1);
    }

}
